package wxdb.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;

public class ChangePasswd extends JDialog{
	
	private JButton chButton = new JButton("Change");
	private JButton clButton = new JButton("Cancel");
	private JLabel chLabel = new JLabel("Change Password :");
	private JLabel pwdLabel1 = new JLabel("New Password :");
	private JLabel pwdLabel2 = new JLabel("Input Password Again :");
	
	private JPasswordField pwd1 = new JPasswordField(10);
	private JPasswordField pwd2 = new JPasswordField(10);
	
	private JComboBox cmb = new JComboBox();   

	private JPanel pane = new JPanel();
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);

		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(chLabel, constraints);
		pane.add(chLabel);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(pwdLabel1, constraints);
		pane.add(pwdLabel1);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(pwd1, constraints);
		pane.add(pwd1);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(pwdLabel2, constraints);
		pane.add(pwdLabel2);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(pwd2, constraints);
		pane.add(pwd2);
		
		constraints=new GridBagConstraints();
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(cmb, constraints);
		pane.add(cmb);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(chButton, constraints);
		pane.add(chButton);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(clButton, constraints);
		pane.add(clButton);
		
		setLocation(400,200);
		setContentPane(pane);
	}
	
	private void initListener(){
		ActListener actListener = new ActListener();
		chButton.addActionListener(actListener);
		clButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == chButton)
				chButtonRun();
			if(e.getSource() == clButton)
				dispose();
		}
	}
	
	private void chButtonRun(){
		String pwds1 = new String(pwd1.getPassword());
		String pwds2 = new String(pwd2.getPassword());
		if(pwds1.equals("") || pwds2.equals("")){
			JOptionPane.showMessageDialog(null,"Please input your password","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(pwds1.equals(pwds2))
			Global.changePasswd((String)cmb.getSelectedItem(), new String(pwd1.getPassword ()));
		else{
			JOptionPane.showMessageDialog(null,"Password wrong","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		this.dispose();
	}
	
	public ChangePasswd(){
		ArrayList<String> al = Global.getUsers();
		for (Iterator<String> iter = al.iterator(); iter.hasNext();) {
			String name = iter.next(); 
			if(name.equals("Administrator"))
				continue;
			cmb.addItem(name);
		}
		setTitle("Administrator-ChangePassword");
		setSize(300,300);
		initListener();
		setMyPane();
		setVisible(true);
		setResizable(false);		
	}
}
